"""
CPEM Secure XML RPC Server (lib/SecureXMLRPCServer.py) 

These bits were taken from http://code.activestate.com/recipes/496786
Minor modifications by C.J. Steele <coreyjsteele@gmail.com>
"""

import SocketServer
import BaseHTTPServer
import SimpleHTTPServer
import SimpleXMLRPCServer
import inspect
import socket,os
from OpenSSL import SSL
from SecureXMLRpcRequestHandler import *

class SecureXMLRPCServer(BaseHTTPServer.HTTPServer,SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
    def __init__(self,server_address,HandlerClass,certFile='certs/cert.pem',keyFile='certs/key.pem',logRequests=True):
        """Secure XML-RPC server.
        It it very similar to SimpleXMLRPCServer but it uses HTTPS for transporting XML data.
        """
        #TODO: sort-out the logging so it uses Logger or similar/trivial mechanism
        self.logRequests = logRequests
        SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self,True,None)
        SocketServer.BaseServer.__init__(self,server_address,HandlerClass)
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.use_privatekey_file (certFile)
        ctx.use_certificate_file(keyFile)
        self.socket = SSL.Connection(ctx,socket.socket(self.address_family,self.socket_type))
        self.server_bind()
        self.server_activate()
